package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Service
@Slf4j
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void addApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("开始处理【添加申请】的业务，参数：{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setCreateTime(new Date());
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        applicationMapper.insertApplication(application);
        /* 遇到的问题:新增申请单对应的审批单时,审批单数据没有此申请单id原因:执行insert方法的SQL时,
        并没有把刚刚生成的申请单id回填到application对象中解决办法:
        给此SQL上加useGeneratedKeys="true" keyProperty="id"属性效果:
        JDBC自动回填此申请单id到application对象的id属性中,再传给审批,审批就拿到申请单id了*/
        //调用审批业务层
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("开始处理【查询申请】的业务，参数：{}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for (int i = 0;i < list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancelApplication(Long id) {
        log.debug("开始处理【取消申请】的业务，参数：{}", id);
        auditMapper.deleteAuditByApplicationId(id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.updateApplication(application);
    }

    @Override
    public void distributeApplication(Long applicationId, Long vehicleId) {
        log.debug("开始处理【分配车辆】的业务，参数:applicationId:{},vehicleId:{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.updateApplication(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public void backApplication(Long applicationId, Long vehicleId) {
        log.debug("开始处理【归还车辆】的业务，参数:applicationId:{},vehicleId:{}", applicationId, vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.updateApplication(application);
    }

    /*
    为当前传入的申请单V0补全审批人数据,此方法并不会产生额外的申请单V0!
    * List<Long> auditUserIdList;
    * //审批人id集合 [106,103]String auditUsernameList;
    //审批人姓名字符串“moly,tom"
    */
    private void assignAuditUserList(ApplicationVO applicationVO){
        log.debug("开始处理【分配审批用户列表】的业务");
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();

        List<AuditVO> auditUserList = auditService.selectAuditByApplicationId(applicationVO.getId());
        for (int i = 0;i < auditUserList.size();i++){
            AuditVO auditVO = auditUserList.get(i);
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String auditUsername : auditUsernameList){
            stringJoiner.add(auditUsername);
        }
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
